﻿using Facebook;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;

namespace FServer
{
    public class FbEngine
    {
        
        public string GetUserID(string accessToken)
        {
            FacebookClient fb = new FacebookClient(accessToken);
            dynamic myData = fb.Get("/me");
            return myData.id;
        }

        public string GetUserName(string accessToken)
        {
            FacebookClient fb = new FacebookClient(accessToken);
            dynamic myData = fb.Get("/me");
            return myData.name;
        }

        public FbAcc GetUser(string accessToken)
        {
            FacebookClient fb = new FacebookClient(accessToken);
            dynamic myData = fb.Get("/me");
            if (myData != null)
            {
                FbAcc owner = new FbAcc();
                //get name
                owner.Name = myData.name;

                //get school
                dynamic schoolList = myData.education;
                for (int i = 0; i < myData.education.Count; i++)
                {
                    if (schoolList[i].type == "College")
                    {
                        owner.School = schoolList[i].school.name;
                        break;
                    }
                }

                //get live
                owner.Live = myData.location.name;

                //get Link
                owner.Link = myData.link;
                return owner;
            }
            return null;
        }

        public bool Post(string accessToken, string _status, string _link = "")
        {
            try
            {
                FacebookClient fb = new FacebookClient(accessToken);
                fb.Post("/me/feed", new { message = _status, link = _link });
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool PostImage(string accessToken, string status, string imagePath)
        {
            try
            {
                FacebookClient fb = new FacebookClient(accessToken);

                var imgStream = File.OpenRead(imagePath);

                fb.Post("/me/photos", new
                {
                    message = status,

                    file = new FacebookMediaStream
                    {
                        ContentType = "image/jps",
                        FileName = Path.GetFileName(imagePath)
                    }.SetValue(imgStream)
                });
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        internal bool PostStatus(string accessToken, string _status)
        {
            try
            {
                FacebookClient fb = new FacebookClient(accessToken);

                fb.Post("/me/feed", new { message = _status });
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public byte[] ImageToByte(string pathImage)
        {
            byte[] imgByteArr;
            FileStream fs = new FileStream(pathImage, FileMode.Open, FileAccess.Read);

            imgByteArr = new byte[fs.Length];
            fs.Read(imgByteArr, 0, System.Convert.ToInt32(fs.Length));
            fs.Dispose();

            return imgByteArr;
        }

        public Bitmap ByteToImage(byte[] blob)
        {
            MemoryStream ms = new MemoryStream();
            byte[] imgByteArr = blob;

            ms.Write(imgByteArr, 0, Convert.ToInt32(imgByteArr.Length));
            Bitmap img = new Bitmap(ms, false);
            ms.Dispose();

            return img;
        }

        public void GetListFr(string accessToken)
        {
        }

        public void GetListFrAvaiable(string accessToken)
        {
        }

        // lay ten user
        public string Parse(string accessToken, string ID)
        {
            string temp_id = "";
            string id = "";

            for (int i = 1; i < ID.Length; i++)
            {
                temp_id = ID[i].ToString();
                if (temp_id != "@")
                {
                    id += temp_id;
                }
                else
                {
                    i = ID.Length + 1;
                }
            }

            FacebookClient fb = new FacebookClient(accessToken);
            String temp = "/" + id;
            dynamic data = fb.Get(temp);
            return data.name;
        }
    }
}